package com.nsgf.library.ui.page.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DiffUtil;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.state.State;
import com.nsgf.library.BR;
import com.nsgf.library.R;
import com.nsgf.library.data.bean.FacilityBean;
import com.nsgf.library.data.bean.UserInfo;
import com.nsgf.library.data.bean.WorkOrderDetailBean;
import com.nsgf.library.databinding.DialogNsgfOverviewBuildingBinding;
import com.nsgf.library.databinding.FragmentNsgfBuildingBinding;
import com.nsgf.library.databinding.ItemProjectOverviewBuildingBinding;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.adapter.SimpleDataBindingPositionAdapter;
import com.nsgf.library.ui.adapter.ViewHolder;
import com.nsgf.library.ui.page.BaseFragment;
import com.nsgf.library.ui.page.StateHolder;
import com.nsgf.skjz.utils.ToastUtils;
//import com.scwang.smart.refresh.layout.api.RefreshLayout;
//import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;


/**
 * 工程概况-建筑物
 */
public class ProjectOverviewBuildingFragment extends BaseFragment {

    public static ProjectOverviewBuildingFragment newInstance() {
        ProjectOverviewBuildingFragment messageFragment = new ProjectOverviewBuildingFragment();
        Bundle bundle = new Bundle();
        messageFragment.setArguments(bundle);
        return messageFragment;

    }

    private Dialog dialog;
    private DialogNsgfOverviewBuildingBinding buildingBinding;

    private ProjectOverviewBuildingFragmentStates mStates;


    private SimpleDataBindingPositionAdapter<FacilityBean, ItemProjectOverviewBuildingBinding> mAdapter;

    @Override
    protected void initViewModel() {
        mStates = getActivityScopeViewModel(ProjectOverviewBuildingFragmentStates.class);
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);

    }

    private AccountRequester mAccountRequester;

    @Override
    protected DataBindingConfig getDataBindingConfig() {

        mAdapter = new SimpleDataBindingPositionAdapter<FacilityBean,
                ItemProjectOverviewBuildingBinding>(getActivity(), R.layout.item_project_overview_building,
                new DiffUtil.ItemCallback<FacilityBean>() {
                    @Override
                    public boolean areItemsTheSame(@NonNull FacilityBean oldItem, @NonNull FacilityBean newItem) {
                        return false;
                    }

                    @Override
                    public boolean areContentsTheSame(@NonNull FacilityBean oldItem, @NonNull FacilityBean newItem) {
                        return false;
                    }
                }) {
            @Override
            protected void onBindItem(ItemProjectOverviewBuildingBinding binding,
                                      FacilityBean item, ViewHolder holder, int position) {
                item.setNum(position + 1);
                binding.setVm(item);

            }
        };
        return new DataBindingConfig(R.layout.fragment_nsgf_building, BR.vm, mStates)
                .addBindingParam(BR.adapter, mAdapter);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getLifecycle().addObserver(mAccountRequester);
        mBinding = (FragmentNsgfBuildingBinding) getBinding();

        mBinding.recyclerView.setItemAnimator(null);
        getDatas();
//        mBinding.smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
//            @Override
//            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
//
//                getDatas();
//            }
//        });
//        mBinding.smartRefreshLayout.autoRefresh();


        mAdapter.setOnItemClickListener((viewId, item, position) -> {

            buildingBinding.llSpillway.setVisibility(View.GONE);
            buildingBinding.llMainDam.setVisibility(View.GONE);
            buildingBinding.llCulvert.setVisibility(View.GONE);
            buildingBinding.tvDialogFacilityName.setText(item.getFacilityName());
            if ("build.type.1".equals(item.getFacilityType())) {
                buildingBinding.llMainDam.setVisibility(View.VISIBLE);
            } else if ("build.type.2".equals(item.getFacilityType())) {
                buildingBinding.llSpillway.setVisibility(View.VISIBLE);
            } else if ("build.type.3".equals(item.getFacilityType())) {
                buildingBinding.llCulvert.setVisibility(View.VISIBLE);
            }
            mAccountRequester.facilityDetail(item.getFacilityCode(), item.getType());

        });

        dialog = new Dialog(getActivity(), R.style.dialogstyle);
        View view1 = View.inflate(getActivity(), R.layout.dialog_nsgf_overview_building, null);
        buildingBinding = DataBindingUtil.bind(view1);

        mAccountRequester.getFacilityResult().observe(this, dataResult -> {
            if (dataResult.getResponseCode() == 200) {
//            mBinding.smartRefreshLayout.finishRefresh();
                mAdapter.getCurrentList().clear();
                mAdapter.submitList(dataResult.getData());
                if (!dataResult.getData().isEmpty()) {
                    mBinding.notdata.setVisibility(View.GONE);
                } else {
                    mBinding.notdata.setVisibility(View.VISIBLE);
                }
            } else {
                mBinding.notdata.setVisibility(View.VISIBLE);
                ToastUtils.showLongToast(dataResult.getMsg());
            }

        });
        mAccountRequester.getFacilityDetailResult().observe(this, responseStatus -> {
            if (responseStatus.getResponseCode() == 200) {


                buildingBinding.setVm(responseStatus.getData());
                dialog.setContentView(view1);
                dialog.setCancelable(true);
                dialog.setCanceledOnTouchOutside(true);
                Window window = dialog.getWindow();
                window.setBackgroundDrawableResource(android.R.color.transparent);
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.width = getResources().getDisplayMetrics().widthPixels;
                window.setAttributes(lp);
                window.setGravity(Gravity.BOTTOM); // 此处可以设置dialog显示的位置
                dialog.show();
            } else {
                ToastUtils.showShortToast(responseStatus.getMsg());
            }
        });


    }

    private FragmentNsgfBuildingBinding mBinding;

    public void getDatas() {
        mAccountRequester.facilityList(UserInfo.getInstance().getProjectCode());
    }

    public static class ProjectOverviewBuildingFragmentStates extends StateHolder {

        public final State<String> name = new State<>("");

        public final State<List<WorkOrderDetailBean.DataBean.PtWorkOrderAbnormal>> list = new State<>(new ArrayList<>());

        public final State<Boolean> loadingVisible = new State<>(false);

    }
}
